FROM python:3.9-slim-buster AS Builder
RUN sed -i -E "s/\w+.debian.org/mirrors.tuna.tsinghua.edu.cn/g" /etc/apt/sources.list
RUN apt-get update && \
    apt-get install -y libtbb-dev libboost-dev libtbb-dev libopenblas-dev && \
    apt-get remove --purge -y && \
    rm -rf /var/lib/apt/lists/*
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip install milvus==2.1.0 --no-deps && \
    pip cache purge

FROM python:3.9-slim-buster
LABEL maintainer=soulteary@gmail.com
# core libs
COPY --from=builder /usr/local/lib/python3.9/site-packages/milvus/bin/*                 /usr/lib/
COPY --from=builder /usr/local/lib/python3.9/site-packages/milvus/lib/*                 /usr/lib/
# basic deps
COPY --from=builder /usr/lib/x86_64-linux-gnu/libtbb.so.2                               /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so                    /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0                          /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libgfortran.so.5.0.0                      /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0                      /usr/lib/x86_64-linux-gnu/
COPY --from=builder /usr/lib/x86_64-linux-gnu/libatomic.so.1.2.0                        /usr/lib/x86_64-linux-gnu/
RUN ln -s /usr/lib/x86_64-linux-gnu/libtbb.so.2              /usr/lib/x86_64-linux-gnu/libtbb.so            && \
    ln -s /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so   /usr/lib/x86_64-linux-gnu/libopenblas.so.0     && \
    ln -s /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so   /usr/lib/x86_64-linux-gnu/libopenblas.so       && \
    ln -s /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0         /usr/lib/x86_64-linux-gnu/libgomp.so.1         && \
    ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.5.0.0     /usr/lib/x86_64-linux-gnu/libgfortran.so.5     && \
    ln -s /usr/lib/x86_64-linux-gnu/libatomic.so.1.2.0       /usr/lib/x86_64-linux-gnu/libatomic.so.1       && \
    ln -s /usr/lib/x86_64-linux-gnu/libquadmath.so.0.0.0     /usr/lib/x86_64-linux-gnu/libquadmath.so.0
# python deps
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple && \
    pip install pymilvus==2.1.0 protobuf==3.20.2 && \
    pip cache purge && \
    mkdir -p /etc/milvus/logs/          && \
    mkdir -p /etc/milvus/data/          && \
    mkdir -p /etc/milvus/etcd/data/     && \
    mkdir -p /etc/milvus/rdb/data       && \
    mkdir -p /var/bin/e-milvus/configs/ && \
    mkdir -p /var/bin/e-milvus/lib/     && \
    chmod 0700 /etc/milvus/etcd/data
# assets
COPY config.yml /var/bin/e-milvus/configs/embedded-milvus.yaml
COPY entrypoint.sh /
CMD ["/entrypoint.sh"]
WORKDIR /app
COPY hello-world.py ./